//
//  main.c
//  12 chapter
//
//  Created by edz on 2020/7/3.
//  Copyright © 2020 edz. All rights reserved.
//

#include <stdio.h>


#if 0
// 练习12-1
#define NAME_LEN 64

struct student {
    char name[NAME_LEN];   /*姓名*/
    int height;            /*身高*/
    float weight;          /*体重*/
    long schols;           /*奖学金*/
};

int main(void)
{
    struct student takao = {"Takao", 173, 86.2};

    printf("姓名 = %s\n", takao.name);
    printf("身高 = %d\n", takao.height);
    printf("体重 = %.1f\n", takao.weight);
    printf("奖学金 = %ld\n", takao.schols);
    
    
    printf("姓名 = %x\n", &takao.name);
    printf("身高 = %x\n", &takao.height);
    printf("体重 = %x\n", &takao.weight);
    printf("奖学金 = %x\n", &takao.schols);

    return 0;
}
#endif


#if 0
#define NAME_LEN 64

typedef struct student {        /*student可以省略*/
    char name[NAME_LEN];   /*姓名*/
    int height;            /*身高*/
    float weight;          /*体重*/
    long schols;           /*奖学金*/
} Student;

/*将std指向的学生的身高变为180cm，体重变为80kg*/
void hiriko(Student *std)
{
    if (std->height < 180) std->height = 180;
    if (std->weight > 80) std->weight = 80;
}


int main(void)
{
    Student sanaka ;//= {"Sanaka", 175, 62.5, 73000};
    
    scanf("%s",sanaka.name);
    scanf("%d",&sanaka.height);
    scanf("%f",&sanaka.weight);
    scanf("%ld",&sanaka.schols);
    hiriko(&sanaka);

    printf("姓名 = %s\n", sanaka.name);
    printf("身高 = %d\n", sanaka.height);
    printf("体重 = %.1f\n", sanaka.weight);
    printf("奖学金 = %ld\n", sanaka.schols);

    return 0;
}
#endif


#if 0
// 练习12-3

/*---从键盘输入int型，long型和double型的值，将这些值作为xyz结构体的成员，返回该结构体的值。---*/
 
#include <stdio.h>
 
struct xyz {
    int a;
    double b;
    long c;
};
 
struct xyz scan_xyz(int a, double b, long c) {
    struct xyz s1;
    
    s1.a = a;
    s1.b = b;
    s1.c = c;
    
    return s1;
}
 
int main (void) {
    int a;
    double b;
    long c;
    struct xyz s2 = {
        0, 0, 0
    };
    
    scanf("%d %lf %ld", &a, &b, &c);
    
    s2 = scan_xyz(a, b, c);
    
    printf("%d %f %ld", s2.a, s2.b, s2.c);
    
    return 0;
}
#endif



#if 0
// 练习12-4
 
#include <stdio.h>
#include <string.h>
 
#define NUMBER 5
#define NAME_LEN 64
 
typedef struct {
    char name[NAME_LEN];
    int height;
    float weight;
    long schols;
} Student;
 
void swap_Student (Student *x, Student *y)
{
    Student temp = *x;
    *x = *y;
    *y = temp;
 }
 
void sort_by_height(Student a[], int n)
{
    int i, j, choice;
    
    puts("请输入你的选择：0---按身高升序排列 1---按姓名排序");
    scanf("%d", &choice);
     
    if(choice == 0){
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++)
            if (a[j].height > a[j + 1].height)
                swap_Student(&a[j], &a[j+1]);
    }
}
 
    if(choice == 1) {
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++)
            if (a[j].name[0] > a[j + 1].name[0])
                swap_Student(&a[j], &a[j+1]);
    }
}
}
 
int main (void)
{
    int i;
    Student std[NUMBER];
    
    for (i = 0; i < NUMBER; i++){
        scanf("%s %d %f %ld", std[i].name, &std[i].height, &std[i].weight, &std[i].schols);
    }
 
    sort_by_height(std, NUMBER);
    
    for (i = 0; i < NUMBER; i++)
        printf("%s %d %f %ld\n", std[i].name, std[i].height, std[i].weight, std[i].schols);
    
    return 0;
}
#endif
